package src.edu.wayne.cs.csc4110.hw6.implementations.userPanels;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import src.edu.wayne.cs.csc4110.hw6.implementations.dbase.UserImpl;
import src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine.StateMachine;
import src.edu.wayne.cs.csc4110.hw6.implementations.VideoFrameImpl;
import src.edu.wayne.cs.csc4110.hw6.interfaces.User;

public class UserRegisterPanel extends StateMachinePanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8698825585057025879L;
	JTextField jtfEmail = new JTextField();
	JTextField jtfUserName = new JTextField();
	JPasswordField jtpfUserPassword = new JPasswordField();
	JPasswordField jtpfUserPasswordC = new JPasswordField();
	JButton jbtnSubmit = new JButton("Submit information");
	JButton jbtnCancel = new JButton("Cancel");
	String strUserName = new String();
	String strUserPasswordC = new String();
	
	UserRegisterPanel(StateMachine stateMachine){
		super(stateMachine);
	
		//Create a panel
		this.setLayout(new BorderLayout(0,150));
		//statePanel
		this.setBackground(VideoFrameImpl.getColor());
		this.setForeground(VideoFrameImpl.getColor());
		this.add(new JLabel("Please fill out all of the fields"), BorderLayout.NORTH);
		
		
		//Put buttons on it
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,0,2,2));
		buttonPanel.setBackground(VideoFrameImpl.getColor());
		
		buttonPanel.add(getCancelButton());
		buttonPanel.add(getJbtnSubmit());
		this.add(buttonPanel, BorderLayout.SOUTH);
	
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(0,1,2,2));
		centerPanel.setBackground(VideoFrameImpl.getColor());
		centerPanel.add( getEmailTextField() );
		centerPanel.add( getUserNameTextField() );
		centerPanel.add( getUserPassWordTextField() );
		centerPanel.add( getUserPassWordCTextField() );
		
		this.add(centerPanel, BorderLayout.CENTER);
	
		stateMachine.setDisplayPanel(this, "Register New User");		
	}

		
	private JButton getJbtnSubmit(){
		jbtnSubmit.addActionListener(
				new ActionListener() 
				{
					public void actionPerformed(ActionEvent e)
					{
						if (registerNewUser())
						{
							JOptionPane.showMessageDialog(null,"Your account has been added");
							goToLoggedInState();
						}
					}
				}
			);    	
		
		return jbtnSubmit;
	}
	
	private JPanel getEmailTextField(){
		JPanel emailTextField = new JPanel();
		emailTextField.setLayout(new GridLayout(1,0,2,2));
		emailTextField.add(new JLabel("Email Address:"));
		emailTextField.add(jtfEmail);

		jtfEmail.addFocusListener(

			new FocusListener()
			{
				@Override
				public void focusLost(FocusEvent e)
				{
					strEmail = jtfEmail.getText();
				}

				@Override
				public void focusGained(FocusEvent arg0) {
					
				}
			}
		);
		
		jtfEmail.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					strEmail = jtfEmail.getText();
				}
			}
		);
	 
		return emailTextField;
	}
	
	private JPanel getUserNameTextField(){
		JPanel userNameTextField = new JPanel();
		userNameTextField.setLayout(new GridLayout(1,0,2,2));
		userNameTextField.add(new JLabel("First Name and Last Name:"));
		userNameTextField.add(jtfUserName);

		jtfUserName.addFocusListener(

			new FocusListener()
			{
				@Override
				public void focusLost(FocusEvent e)
				{
					strUserName = jtfUserName.getText();
				}

				@Override
				public void focusGained(FocusEvent arg0) {
					
				}
			}
		);
		
		jtfUserName.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					strUserName = jtfUserName.getText();
				}
			}
		);
		return userNameTextField;
	}
	
	private JPanel getUserPassWordTextField(){
		JPanel userPassWordTextField = new JPanel();
		userPassWordTextField.setLayout(new GridLayout(1,0,2,2));
		userPassWordTextField.add(new JLabel("Password:"));
		userPassWordTextField.add(jtpfUserPassword);

		jtpfUserPassword.addFocusListener(

			new FocusListener()
			{
				@Override
				public void focusLost(FocusEvent e)
				{
					strUserPassword = new String(jtpfUserPassword.getPassword());
				}

				@Override
				public void focusGained(FocusEvent arg0) {
					
				}
			}
		);
		
		jtpfUserPassword.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					strUserPassword = new String(jtpfUserPassword.getPassword());
				}
			}
		);
		
		return userPassWordTextField;
	
	}
	
	private JPanel getUserPassWordCTextField(){
		JPanel userPassWordCTextField = new JPanel();
		userPassWordCTextField.setLayout(new GridLayout(1,0,2,2));
		userPassWordCTextField.add(new JLabel("Confirm Password:"));
		userPassWordCTextField.add(jtpfUserPasswordC);

		jtpfUserPasswordC.addFocusListener(

			new FocusListener()
			{
				@Override
				public void focusLost(FocusEvent e)
				{
					strUserPasswordC = new String(jtpfUserPasswordC.getPassword());
				}

				@Override
				public void focusGained(FocusEvent arg0) {
					
				}
			}
		);
		
		jtpfUserPasswordC.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					strUserPasswordC = new String(jtpfUserPasswordC.getPassword());
				}
			}
		);
		
		return userPassWordCTextField;
	}

	public boolean registerNewUser()
	{
		Boolean result = false;
		if ((strEmail.equals("")) || (strUserName.equals("")) || (strUserPassword.equals("")) || (strUserPasswordC.equals("")) )
		{
			JOptionPane.showMessageDialog(null,"All of the fields must be filled in");
		}
		else if (! strUserPassword.equals(strUserPasswordC))
		{
			JOptionPane.showMessageDialog(null,"Passwords do not match");
		}
		else if (!strEmail.matches("^\\w+@\\w+\\.{1}(com|edu|net|gov)$")){
			JOptionPane.showMessageDialog(null,"Must be a valid email address.");
		}
		else if (stateMachine.getFrameWork().getDataBase().getUser(strEmail) != null)
		{
			JOptionPane.showMessageDialog(null,"Email address alread exists in the dbase.");
		}
		else
		{
			User tempUser = new UserImpl(strEmail, strUserName, strUserPassword);
			result = stateMachine.getFrameWork().getDataBase().register(tempUser);
		}
		return result;
	}
	
	private void goToLoggedInState(){
		User user = getStateMachine().getUser(strEmail);
		getStateMachine().setCurrentUser(user);
		getStateMachine().setState(getStateMachine().getLoggedInState());
		getStateMachine().goToLoggedInState();
	}	
	
	
	private JButton getCancelButton(){
		JButton jbtnCancel = new JButton("Cancel");
		jbtnCancel.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					goToLoggedInState();
    				}
    			}
    		);  
		return jbtnCancel;
	}
}
